
import 'dart:math';

import 'package:get/get.dart';
import 'package:untitled/pages/Getx_project/ShopClassBean.dart';
class ShopController extends GetxController{
  // 定义数组 保存分类数据
  List<ShopClassBean> _classBeanList = [];

  List<ShopClassBean> get classBeanList => _classBeanList;

  // 模拟网络请求
  void requestShopClass() async{
    // 清空分类数据
    _classBeanList = [];
    await Future.delayed(Duration(seconds: 2),);
    for(int i = 0;i <10;i++){
      _classBeanList.add(ShopClassBean(i, "分类$i"));
    }
    update();
  }

  Map<int,List<GoodsBean>> map = Map();
  List<GoodsBean> _currentGoods = [];

  List<GoodsBean> get currentGoods => _currentGoods;

  void requestShopGoodsList(ShopClassBean element) async{
    // 保存商品列表数据
    List<GoodsBean> goodsList = [];
    // 模拟商品列表数据
   if(goodsList == null || goodsList.length == 0){
     goodsList = [];
      for(int i =0;i<50;i++){
        GoodsBean bean = GoodsBean();
        // The argument type 'int?' can't be assigned to the parameter type 'num'
        int elementId = element.id as int;
        bean.id =i+elementId+(Random().nextInt(100)) ;
        bean.title = "超级小水果 ${element.title}";
        goodsList.add(bean);
    }
    // 保存每一个分类中的列表数据
    map[element.id as int] = goodsList;
   }
   _currentGoods = goodsList;
   // 更新当前分类
   update(["${element.id}"]);
  }

  // 定义购物车
  List<int> _cardList = [];
  List<int> get cardList => _cardList;
  void addCard(GoodsBean bean){
    if(!_cardList.contains(bean.id)){
      _cardList.add(bean.id as int);
    }else{
      _cardList.remove(bean.id);
    }
    update(["card"]);
  }
}